package cn.itrip.auth.controller;

import java.util.Calendar;

import cn.itrip.common.MD5;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import springfox.documentation.annotations.ApiIgnore;
/*import cn.itrip.auth.exception.UserLoginFailedException;*/
import cn.itrip.auth.service.TokenService;
import cn.itrip.auth.service.UserService;
import cn.itrip.beans.dto.Dto;
import cn.itrip.beans.pojo.ItripUser;
import cn.itrip.beans.vo.ItripTokenVO;
import cn.itrip.common.DtoUtil;
import cn.itrip.common.EmptyUtils;
import cn.itrip.common.ErrorCode;

/**
 * 用户登录控制器
 * @author hduser
 *
 */
@Api
@Controller
@RequestMapping(value = "/api")
public class LoginController {

	@Resource
	private UserService userService;

	@Resource
	private TokenService tokenService;

	/**
	 * 用户登陆
	 * @param name
	 * @param password
	 * @param request
	 * @return
	 */
	@ApiOperation(value = "用户登陆", notes = "根据用户名,密码进行统一验证", tags = {""}, response = String.class)
	@ApiImplicitParams({
			@ApiImplicitParam(value = "用户名", name = "name", required = true, dataType = "String",paramType = "query"),
			@ApiImplicitParam(value = "密码", name = "password", required = true, dataType = "String",paramType = "query")
	})
	@RequestMapping(value="/dologin",method=RequestMethod.POST,produces= "application/json")
	public @ResponseBody Dto dologin(
			@RequestParam
			String name,
			@RequestParam
			String password,
			HttpServletRequest request) {
		try {
			ItripUser user=userService.login(name,password);
			if (EmptyUtils.isNotEmpty(user)){
				String userAgent=request.getHeader("user-agent");
				String token=tokenService.geterateToken(userAgent,user);//生成token
				tokenService.save(token,user);//保存token

				ItripTokenVO tokenVO=new ItripTokenVO(token,Calendar.getInstance().getTimeInMillis()+2*60*60*1000,Calendar.getInstance().getTimeInMillis());
				return DtoUtil.returnSuccess();
			}else{
				return DtoUtil.returnFail("用户名或密码错误",ErrorCode.AUTH_AUTHENTICATION_FAILED);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return DtoUtil.returnFail(e.getMessage(),ErrorCode.AUTH_AUTHENTICATION_FAILED);
		}
	}

	@ApiOperation(value = "用户注销", notes = "客户端需要在header中发送token", tags = {""}, response = String.class)
	@ApiImplicitParam(value = "token",name="token",required = true, dataType = "String",paramType = "header")
	@RequestMapping(value="/logout",method=RequestMethod.GET,produces="application/json",headers="token")
	public @ResponseBody Dto logout(HttpServletRequest request) {
		String token = request.getHeader("token");
		try {
			if (tokenService.validate(request.getHeader("user-agent"), token)) {
				tokenService.delete(token);
				return DtoUtil.returnSuccess("注销成功！");
			} else {
				return DtoUtil.returnFail("token无效", ErrorCode.AUTH_TOKEN_INVALID);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return DtoUtil.returnFail("退出失败", ErrorCode.AUTH_TOKEN_INVALID);
		}

	}
}
